package top.syfy.spb.upload.imageandwatermark.service.impl;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import top.syfy.spb.upload.imageandwatermark.service.ImageUploadService;

import java.io.File;
import java.io.IOException;
import java.util.Objects;

/**
 * 上传图片的服务层实现
 *
 * @author awg
 * @date 2020/10/23 星期五
 */
@Service
public class ImageUploadServiceImpl implements ImageUploadService {

    /**
     * 上传图片
     *
     * @param img       上传的图片
     * @param uploadDir 上传图片的存储路径
     * @return 已上传的未添加水印的图片的URL相对地址
     */
    @Override
    public String uploadImage(MultipartFile img, String uploadDir) throws IOException {
        String uploadedImgPath = uploadDir + File.separator + Objects.requireNonNull(img.getOriginalFilename());
        FileUtils.writeByteArrayToFile(
                new File(uploadedImgPath),
                img.getBytes()
        );
        return uploadedImgPath;
    }
}
